﻿using SpotOffer.CustomViews;
using SpotOffer.Resource;
using Xamarin.Forms;
using XLabs.Forms.Controls;

namespace SpotOffer.View
{
    public class SettingsPage : BasePage
    {
        private ListView _listViewPictureSize;
        private ExtendedLabel _labelPictureSize;
        private ExtendedLabel _labelOpenDeal;
        private ListView _listViewOpenDeal;
        public RelativeLayout SettingsLayout { get; set; }

      
        protected override void OnAppearing()
        {
            base.OnAppearing();   

            SetBinding(TitleProperty, new Binding("Title"));
            SettingsLayout = new RelativeLayout
            {
                BackgroundColor = AppColors.White,
            };

            MakeOpenDealSettings();
            MakePictureSizeSettings();
            AddToContent();


            Content = SettingsLayout;
        }

        private void AddToContent()
        {
            SettingsLayout.Children.Add(_labelOpenDeal,
                Constraint.Constant(0),
                Constraint.Constant(1),
                Constraint.RelativeToParent((parent) => parent.Bounds.Width));

            SettingsLayout.Children.Add(_listViewOpenDeal,
                Constraint.Constant(0),
                Constraint.RelativeToView(_labelOpenDeal, (parent, sibling) => sibling.Bounds.Bottom),
                Constraint.RelativeToParent((parent) => parent.Bounds.Width));

            SettingsLayout.Children.Add(_labelPictureSize,
                Constraint.Constant(0),
                Constraint.RelativeToView(_listViewOpenDeal, (parent, sibling) => sibling.Bounds.Bottom + 50),
                Constraint.RelativeToParent((parent) => parent.Bounds.Width));

            SettingsLayout.Children.Add(_listViewPictureSize,
                Constraint.Constant(0),
                Constraint.RelativeToView(_labelPictureSize, (parent, sibling) => sibling.Bounds.Bottom),
                Constraint.RelativeToParent((parent) => parent.Bounds.Width));

        }

        private void MakePictureSizeSettings()
        {
            _listViewPictureSize = AppViews.ReturnListView();
            _listViewPictureSize.VerticalOptions = LayoutOptions.Fill;
            _listViewPictureSize.BackgroundColor = AppColors.White;
            _listViewPictureSize.SetBinding(ListView.ItemsSourceProperty, new Binding("PictureSizeList"));
            _listViewPictureSize.SetBinding(ListView.SelectedItemProperty, new Binding("SelectedPictureSizeSetting", BindingMode.TwoWay));
            _listViewPictureSize.ItemTemplate = new DataTemplate(() =>
            {
                var settingLabel = AppViews.ItemLabel();
                settingLabel.SetBinding(Label.TextProperty, "text");
                settingLabel.SetBinding(Label.TextColorProperty, "ItemTextColor");
  
                var finalview = AppViews.ItemContentView(settingLabel);
                finalview.SetBinding(BackgroundColorProperty, "ItemBackgroundColor");

                return new ViewCell
                {
                    View = finalview
                };
            });

            _labelPictureSize = new ExtendedLabel
            {
                BackgroundColor = AppColors.White,
                TextColor = AppColors.Grey,
                YAlign = TextAlignment.Center,
                XAlign = TextAlignment.Center,
                HeightRequest = 40,
                FontAttributes = FontAttributes.Italic,
                FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)),
            };
            _labelPictureSize.SetBinding(Label.TextProperty, new Binding("TitlePictureSize"));

        }

        private void MakeOpenDealSettings()
        {
            _listViewOpenDeal = AppViews.ReturnListView();
            _listViewOpenDeal.SetBinding(ListView.ItemsSourceProperty, new Binding("OpenDealList"));
            _listViewOpenDeal.SetBinding(ListView.SelectedItemProperty, new Binding("SelectedOpenSetting", BindingMode.TwoWay));
            _listViewOpenDeal.ItemTemplate = new DataTemplate(() =>
            {
                var settingLabel = AppViews.ItemLabel();
                settingLabel.SetBinding(Label.TextProperty, "text");
                settingLabel.SetBinding(Label.TextColorProperty, "ItemTextColor");

                var finalview = AppViews.ItemContentView(settingLabel);
                finalview.SetBinding(BackgroundColorProperty, "ItemBackgroundColor");

                return new ViewCell
                {
                    View = finalview
                };
            });
            _listViewOpenDeal.VerticalOptions = LayoutOptions.Fill;
            _listViewOpenDeal.HorizontalOptions=LayoutOptions.FillAndExpand;

            _labelOpenDeal = new ExtendedLabel {
                BackgroundColor = AppColors.White,
                TextColor = AppColors.Grey,
                YAlign = TextAlignment.Center,
                XAlign = TextAlignment.Center,
                FontAttributes = FontAttributes.Italic,
                HeightRequest = 40,
                FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)),
            };
            _labelOpenDeal.SetBinding(Label.TextProperty, new Binding("TitleOpenDeal"));

        }

    }
}
